// --------------------------------------------------------------------------------------------------------------------
// <copyright company="" file="BooleanToVisibilityConverter.cs">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Convert a bool to Visibility
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.View.Controls.Converters
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;

    /// <summary>
    /// Convert a bool to Visibility
    /// </summary>
    public sealed class BooleanToVisibilityConverter : IValueConverter
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BooleanToVisibilityConverter"/> class. 
        /// Standard constructor
        /// </summary>
        public BooleanToVisibilityConverter()
        {
            this.TrueValue = Visibility.Visible;
            this.FalseValue = Visibility.Collapsed;
            this.UnsetValue = Visibility.Collapsed;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The value to return when the boolean is false
        /// </summary>
        /// <remarks>
        /// Defaults to Collapsed
        /// </remarks>
        public Visibility FalseValue { get; set; }

        /// <summary>
        /// The value to return when the boolean is true
        /// </summary>
        /// <remarks>
        /// Defaults to Visible
        /// </remarks>
        public Visibility TrueValue { get; set; }

        /// <summary>
        /// The value to return when the boolean is null/unset
        /// </summary>
        /// <remarks>
        /// Defaults to Collapsed
        /// </remarks>
        public Visibility UnsetValue { get; set; }

        #endregion

        #region Implemented Interfaces

        #region IValueConverter

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <param name="targetType">
        /// The target type.
        /// </param>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        /// <param name="culture">
        /// The culture.
        /// </param>
        /// <returns>
        /// The convert.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value == DependencyProperty.UnsetValue)
            {
                return this.UnsetValue;
            }

            var logic = (bool)value;
            if (logic)
            {
                return this.TrueValue;
            }
            else
            {
                return this.FalseValue;
            }
        }

        /// <summary>
        /// The convert back.
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <param name="targetType">
        /// The target type.
        /// </param>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        /// <param name="culture">
        /// The culture.
        /// </param>
        /// <returns>
        /// The convert back.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        #endregion

        #endregion
    }
}